package com.manage.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.assessment.entity.AssessmentProject;
import com.assessment.entity.AssessmentTemplate;
import com.manage.dao.CourseRepository;
import com.manage.dao.TrainingCourseRepository;
import com.manage.dao.TrainingRecordRepository;
import com.manage.entity.Course;
import com.manage.entity.TrainingCourse;
import com.manage.entity.TrainingRecord;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;
import com.staff.entity.dto.StaffQueryDTO;

@Service
@Transactional
public class TrainingCourseService implements ITrainingCourseService{
	
	@Autowired
	private TrainingCourseRepository trainingCourseRepository;
	@Autowired
	private StaffRepository staffRepository;
	@Autowired
	private CourseRepository courseRepository;
	@Autowired
	private ITrainingRecordService trainingRecordService;
	
	public void saveOrUpdate(TrainingCourse trainingCourse) {
		Staff responsiblePerson=staffRepository.findOne(trainingCourse.getResponsiblePersonId());
		Course course =courseRepository.findOne(trainingCourse.getCoursesId());
		trainingCourse.setResponsiblePerson(responsiblePerson);
		trainingCourse.setCourses(course);
		trainingCourseRepository.save(trainingCourse);
	}
	
	public void save(TrainingCourse trainingCourse) {
		trainingCourseRepository.save(trainingCourse);
	}
	public void delete(Long id) {		
		TrainingCourse trainingCourse=trainingCourseRepository.findOne(id);
		if(trainingCourse!=null) {
			List<TrainingRecord> trList=trainingCourseRepository.findTrainingRecordByTrainingCourseId(id);
			for(TrainingRecord trainingRecord:trList) {
				trainingRecordService.delete(trainingRecord.getId());
			}		
			trainingCourse.setCourses(null);
			trainingCourse.setResponsiblePerson(null);
			trainingCourseRepository.save(trainingCourse);
			trainingCourseRepository.delete(id);
		}		
	}
	
	
	public void delete(List<Long> ids) {
		for(Long id:ids) {
			this.delete(id);
		}
	}
	
	@Transactional(readOnly = true)
	public TrainingCourse findOne(Long id) {
		return trainingCourseRepository.findOne(id);
	}
	
	@Transactional(readOnly = true)
	public List<TrainingCourse> findAll(){
		return (List<TrainingCourse>) trainingCourseRepository.findAll();
	}
	
	@Transactional(readOnly = true)
	public Page<TrainingCourse> findAll(TrainingCourse trainingCourse, Pageable pageable){
		Page<TrainingCourse> page=trainingCourseRepository.findAll(TrainingCourse.getSpecification(trainingCourse), pageable);
		List<TrainingCourse> list=page.getContent();
		for(int i=0;i<list.size();i++) {
			Long responsiblePersonId= list.get(i).getResponsiblePerson().getId();
			String responsiblePersonName=list.get(i).getResponsiblePerson().getStaffName();
			Long coursesId=list.get(i).getCourses().getId();	
			String coursesName=list.get(i).getCourses().getCoursesName();
			list.get(i).setResponsiblePersonId(responsiblePersonId);;
			list.get(i).setResponsiblePersonName(responsiblePersonName);;
			list.get(i).setCoursesId(coursesId);
			list.get(i).setCoursesName(coursesName);
			Date data=new Date();
			if(list.get(i).getStartTime()!=null&&list.get(i).getEndTime()!=null) {
				if(data.after(list.get(i).getStartTime())&&data.before(list.get(i).getEndTime())) {			
					list.get(i).setExamine("执行");
				}
				else if(data.before(list.get(i).getStartTime())) {
					list.get(i).setExamine("未开始");
				}
				else if(data.after(list.get(i).getEndTime())){
					list.get(i).setExamine("结束");
				}
			}
			else {
				list.get(i).setExamine("未设置");
			}
			int count=trainingCourseRepository.findCount(list.get(i).getId());
			list.get(i).setCount(count);
		}
		Page<TrainingCourse> page1 = new PageImpl<TrainingCourse>(list,pageable, page.getTotalElements());
		return page1;
	}

}
